//
//  AppTvCaseDetailView.m
//  classicFilms
//
//  Created by 刘继丹 on 17/4/25.
//  Copyright © 2017年 Stefan Arn. All rights reserved.
//

#import "AppTvCaseDetailView.h"
#import <Masonry.h>
#import "KeyHeader.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
#import "ARNFocusTextView.h"

@interface AppTvCaseDetailView()<ARNFocusTextViewDelegate>
{
    NSString *content;
}
@property(nonatomic, strong) UILabel *mNameLabel, *mPriceLabel;
@property(nonatomic, strong) UIImageView *mInclusivePImageView;

@property(nonatomic, strong) UIView *mLine;

@property(nonatomic, strong) UIImageView *mDetailImageView;
@property(nonatomic, strong) ARNFocusTextView  *mDetailLabel;

@property(nonatomic, strong) UILabel *mDetailTitleLabel;

@property(nonatomic, strong) UIImageView *mHeaderImageView;

@property(nonatomic, strong) UIMotionEffectGroup *motionEffectGroup;

@end

@implementation AppTvCaseDetailView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = kDetailTopBackGroundColor_black;
        
        [self setupViews];
        [self setUpdateConstraints];
        
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    if (_mDetailLabel.text != 0) {
        NSString *subtitle = _mDetailLabel.text;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:subtitle];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:18];//调整行间距
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [subtitle length])];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, [subtitle length])];
        [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:NSMakeRange(0, [subtitle length])];
        self.mDetailLabel.attributedText = attributedString;
    }
    
}

-(void)setUpdateConstraints{
    
    [_mNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).with.offset(170);
        make.top.mas_equalTo(self.mas_top).with.offset(220);
        make.height.mas_equalTo(48);
    }];
    
    UIImage *image = [UIImage imageNamed:@"inclusive_price"];
    [_mInclusivePImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_mNameLabel.mas_left);
        make.top.mas_equalTo(_mNameLabel.mas_bottom).with.offset(31);
        make.size.mas_equalTo(CGSizeMake(image.size.width, image.size.height));
    }];
    
    [_mPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_mInclusivePImageView.mas_right).with.offset(16);
        make.top.mas_equalTo(_mInclusivePImageView.mas_top).with.offset(-1);
        make.height.mas_equalTo(34);
    }];
    
    
    [_mLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_mInclusivePImageView.mas_left);
        make.top.mas_equalTo(_mInclusivePImageView.mas_bottom).with.offset(50);
        make.size.mas_equalTo(CGSizeMake(KVIEW_W-1230, 0.5));
    }];
    
    UIImage *detailImage = [UIImage imageNamed:@"ico_special"];
    [_mDetailImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_mLine.mas_left);
        make.top.mas_equalTo(_mLine.mas_bottom).with.offset(50);
        make.size.mas_equalTo(CGSizeMake(detailImage.size.width, detailImage.size.height));
    }];
    
    [_mDetailTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_mDetailImageView.mas_right).with.offset(10);
        make.top.mas_equalTo(_mDetailImageView.mas_top).with.offset(1);
        make.height.mas_equalTo(24);
    }];
    
    [_mHeaderImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top).with.offset(160);
        make.right.mas_equalTo(self.mas_right).with.offset(-130);
        make.bottom.mas_equalTo(self.mas_bottom).with.offset(-110);
        make.width.mas_equalTo(800);
    }];
    
}

-(void)setupViews{
    [self addSubview:self.mInclusivePImageView];
    [self addSubview:self.mNameLabel];
    [self addSubview:self.mPriceLabel];
    [self addSubview:self.mLine];
    [self addSubview:self.mDetailImageView];
    [self addSubview:self.mDetailTitleLabel];
    [self addSubview:self.mHeaderImageView];
}

-(void)setDic:(NSDictionary *)dic{
    if (_dic != dic) {
        _dic = dic;
    }
    
    NSString *title = [_dic valueForKey:@"title"];
    NSNumber *price = [_dic valueForKey:@"price"];
    NSString *desc = [_dic valueForKey:@"desc"];
    NSString *cover = [_dic valueForKey:@"cover_url"];

    _mNameLabel.text = title;
    _mPriceLabel.text = [NSString stringWithFormat:@"¥%@", price.stringValue];
    
    content = desc;
    
    if (content.length != 0) {
        [self addSubview:self.mDetailLabel];
        _mDetailLabel.text = content;
        
        [self.mDetailLabel isTouchable:[self isFocusTextViewTruncated]];
        
    }
    
    [_mHeaderImageView sd_setImageWithURL:[NSURL URLWithString:cover]];
    
}

-(UILabel *)mNameLabel{
    if (!_mNameLabel) {
        _mNameLabel = [[UILabel alloc]init];
        _mNameLabel.textColor = [UIColor whiteColor];
        _mNameLabel.font = [UIFont systemFontOfSize:48];
    }
    return _mNameLabel;
}

-(UILabel *)mPriceLabel{
    if (!_mPriceLabel) {
        _mPriceLabel = [[UILabel alloc]init];
        _mPriceLabel.textColor = kColorRed;
        _mPriceLabel.font = [UIFont systemFontOfSize:34];
    }
    return _mPriceLabel;
}

-(UIImageView *)mInclusivePImageView{
    if (!_mInclusivePImageView) {
        _mInclusivePImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"inclusive_price"]];
    }
    return _mInclusivePImageView;
}

-(UIView *)mLine{
    if (!_mLine) {
        _mLine = [[UIView alloc]init];
        _mLine.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.12];
    }
    return _mLine;
}

-(UIImageView *)mDetailImageView{
    if (!_mDetailImageView) {
        _mDetailImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ico_special"]];
    }
    return _mDetailImageView;
}

-(UILabel *)mDetailTitleLabel{
    if (!_mDetailTitleLabel) {
        _mDetailTitleLabel = [[UILabel alloc]init];
        _mDetailTitleLabel.textColor = [UIColor whiteColor];
        _mDetailTitleLabel.font = [UIFont systemFontOfSize:24];
        _mDetailTitleLabel.text = @"亮点";
    }
    return _mDetailTitleLabel;
}

-(ARNFocusTextView *)mDetailLabel{
    if (!_mDetailLabel) {
        _mDetailLabel = [[ARNFocusTextView alloc]initWithFrame:CGRectMake(150, 480, KVIEW_W-1230, 180)];
        _mDetailLabel.textColor = [UIColor whiteColor];
        _mDetailLabel.font = [UIFont systemFontOfSize:22];
        _mDetailLabel.textContainer.maximumNumberOfLines = 0;
        _mDetailLabel.textColor = [UIColor whiteColor];
        _mDetailLabel.layer.shadowColor = [[UIColor colorWithHexString:@"0x191b27"]CGColor];
        _mDetailLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
        _mDetailLabel.layer.shadowOpacity = 2.0f;
        _mDetailLabel.layer.shadowRadius = 2.0f;
        _mDetailLabel.focusTextViewDelegate = self;
    }
    return _mDetailLabel;
}

- (BOOL)isFocusTextViewTruncated {
    BOOL isFocusTextViewTruncated = NO;
    
    CGFloat padding = self.mDetailLabel.textContainer.lineFragmentPadding;
    CGFloat actualPageWidth = self.mDetailLabel.textContainer.size.width - padding * 2;
    
    CGSize textSize = [self.mDetailLabel.text boundingRectWithSize:CGSizeMake(actualPageWidth, CGFLOAT_MAX)
                                                           options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                                        attributes:@{NSFontAttributeName:self.mDetailLabel.font}
                                                           context:nil].size;
    
    if (ceilf(textSize.height) > (self.mDetailLabel.textContainer.size.height)) {
        isFocusTextViewTruncated = YES;
    }
    
    return isFocusTextViewTruncated;
}

- (void)focusTextViewClicked {
    if (_detailLabelTouchBlock) {
        _detailLabelTouchBlock(content);
    }
}

-(UIImageView *)mHeaderImageView{
    if (!_mHeaderImageView) {
        _mHeaderImageView = [[UIImageView alloc]init];
        _mHeaderImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _mHeaderImageView;
}

@end

